package com.xuef.controller;

import com.google.common.collect.Lists;
import com.xuef.base.ApiResponse;
import com.xuef.base.Cons;
import com.xuef.entity.Blog;
import com.xuef.entity.Comment;
import com.xuef.entity.User;
import com.xuef.service.BlogService;
import com.xuef.service.IUserService;
import com.xuef.vo.CommentVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by moveb on 2018/5/23.
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

    @Autowired
    private IUserService userService;
    @Autowired
    private BlogService blogService;

    /**
     * 发表评论
     * @param blogId
     * @param commentContent
     * @param authentication
     * @return
     */
    @PostMapping("/blog")
    @ResponseBody
    public ApiResponse addComment(@RequestParam("blogId") Integer blogId,
                                  @RequestParam String commentContent,
                                  Authentication authentication){
        User user = null;
        logger.info(blogId + "  " + commentContent);
        if(authentication != null && authentication.isAuthenticated()){
            String username = authentication.getName();
            user = userService.findByUsername(username);
            Comment comment = new Comment(user.getId(), commentContent);
            comment.setCreateTime(new Date());
            // 评论的是 博客
            comment.setEntityType(Cons.ENTITY_TYPE.ENTITY_BLOG);
            comment.setEntityId(blogId);
            comment.setStatus(0);
            comment.setUser(user);

            // 评论保存到mysql
            comment = blogService.saveComment(comment);
            return ApiResponse.ofSuccess(comment);
        }else{
            // 用户没有登录
            return ApiResponse.ofMessage(403, "未登录");
        }
    }

    @GetMapping("/blog")
//    @ResponseBody
    public String listComments(@RequestParam("blogId") Integer blogId,
                               Model model,
                               Authentication authentication){

        List<Comment> comments = null;
        comments = blogService.listComments(Cons.ENTITY_TYPE.ENTITY_BLOG, blogId);
        String loginUsername = "";
        if(authentication != null && authentication.isAuthenticated()) {
            loginUsername = authentication.getName();
        }
        List<CommentVO> commentVOs = Lists.newArrayList();
        boolean isOwner = false;
        for(Comment c:comments){
            isOwner = c.getUser().getName().equals(loginUsername) ? true : false;
            CommentVO commentVO = new CommentVO(c, isOwner);
            commentVOs.add(commentVO);
        }
        model.addAttribute("comments", commentVOs);
        //return ApiResponse.ofSuccess(comments);
        return "userspace/blog :: #mainContainerRepleace";
    }
}
